package com.orange.common.android.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.orange.score.utils.DateUtil;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class PullToRefreshExpandableListView extends ExpandableListView
  implements AbsListView.OnScrollListener
{
  private static final int PULL_TO_REFRESH = 2;
  private static final int REFRESHING = 4;
  private static final int RELEASE_TO_REFRESH = 3;
  private static final String TAG = "PullToRefreshExpandableListView";
  private static final int TAP_TO_REFRESH = 1;
  private boolean firstLoad = true;
  private boolean isInInitState;
  private int mCurrentScrollState;
  private RotateAnimation mFlipAnimation;
  private LayoutInflater mInflater;
  private int mLastMotionY;
  private OnRefreshListener mOnRefreshListener;
  private AbsListView.OnScrollListener mOnScrollListener;
  private int mRefreshOriginalTopPadding;
  private int mRefreshState;
  private RelativeLayout mRefreshView;
  private int mRefreshViewHeight;
  private ImageView mRefreshViewImage;
  private TextView mRefreshViewLastUpdated;
  private ProgressBar mRefreshViewProgress;
  private TextView mRefreshViewText;
  private RotateAnimation mReverseFlipAnimation;

  public PullToRefreshExpandableListView(Context paramContext)
  {
    super(paramContext);
    init(paramContext);
  }

  public PullToRefreshExpandableListView(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    init(paramContext);
  }

  public PullToRefreshExpandableListView(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
  {
    super(paramContext, paramAttributeSet, paramInt);
    init(paramContext);
  }

  private void applyHeaderPadding(MotionEvent paramMotionEvent)
  {
    int k;
    int i = paramMotionEvent.getHistorySize();
    int j = 1;
    try
    {
      int i4 = ((Integer)MotionEvent.class.getMethod("getPointerCount", new Class[0]).invoke(paramMotionEvent, new Object[0])).intValue();
      j = i4;
      k = 0;
      if (k >= i)
        return;
    }
    catch (NoSuchMethodException localNoSuchMethodException2)
    {
      j = 1;
    }
    catch (IllegalArgumentException localIllegalArgumentException2)
    {
      throw localIllegalArgumentException2;
    }
    catch (IllegalAccessException localIllegalAccessException2)
    {
      System.err.println("unexpected " + localIllegalAccessException2);
    }
    catch (InvocationTargetException localInvocationTargetException1)
    {
      int l;
      int i1;
      while (true)
      {
        while (true)
          System.err.println("unexpected " + localInvocationTargetException1);
        l = 0;
        if (l < j)
          break;
        ++k;
      }
      if (this.mRefreshState == 3)
      {
        if (isVerticalFadingEdgeEnabled())
          setVerticalScrollBarEnabled(false);
        i1 = 0;
      }
      try
      {
        Class[] arrayOfClass = new Class[2];
        arrayOfClass[0] = Integer.TYPE;
        arrayOfClass[1] = Integer.TYPE;
        Method localMethod = MotionEvent.class.getMethod("getHistoricalY", arrayOfClass);
        Object[] arrayOfObject = new Object[2];
        arrayOfObject[0] = Integer.valueOf(l);
        arrayOfObject[1] = Integer.valueOf(k);
        int i3 = ((Float)localMethod.invoke(paramMotionEvent, arrayOfObject)).intValue();
        i1 = i3;
        int i2 = (int)((i1 - this.mLastMotionY - this.mRefreshViewHeight) / 1.7D);
        this.mRefreshView.setPadding(this.mRefreshView.getPaddingLeft(), i2, this.mRefreshView.getPaddingRight(), this.mRefreshView.getPaddingBottom());
        ++l;
      }
      catch (NoSuchMethodException localNoSuchMethodException1)
      {
        while (true)
          i1 = (int)paramMotionEvent.getHistoricalY(k);
      }
      catch (IllegalArgumentException localIllegalArgumentException1)
      {
        throw localIllegalArgumentException1;
      }
      catch (IllegalAccessException localIllegalAccessException1)
      {
        while (true)
          System.err.println("unexpected " + localIllegalAccessException1);
      }
      catch (InvocationTargetException localInvocationTargetException2)
      {
        while (true)
          System.err.println("unexpected " + localInvocationTargetException2);
      }
    }
  }

  private void init(Context paramContext)
  {
    this.mFlipAnimation = new RotateAnimation(0F, -180.0F, 1, 0.5F, 1, 0.5F);
    this.mFlipAnimation.setInterpolator(new LinearInterpolator());
    this.mFlipAnimation.setDuration(250L);
    this.mFlipAnimation.setFillAfter(true);
    this.mReverseFlipAnimation = new RotateAnimation(-180.0F, 0F, 1, 0.5F, 1, 0.5F);
    this.mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
    this.mReverseFlipAnimation.setDuration(250L);
    this.mReverseFlipAnimation.setFillAfter(true);
    this.mInflater = ((LayoutInflater)paramContext.getSystemService("layout_inflater"));
    this.mRefreshView = ((RelativeLayout)this.mInflater.inflate(2130903081, this, false));
    this.mRefreshViewText = ((TextView)this.mRefreshView.findViewById(2131558510));
    this.mRefreshViewImage = ((ImageView)this.mRefreshView.findViewById(2131558509));
    this.mRefreshViewProgress = ((ProgressBar)this.mRefreshView.findViewById(2131558508));
    this.mRefreshViewLastUpdated = ((TextView)this.mRefreshView.findViewById(2131558511));
    this.mRefreshViewImage.setMinimumHeight(50);
    this.mRefreshView.setOnClickListener(new OnClickRefreshListener(this, null));
    this.mRefreshOriginalTopPadding = this.mRefreshView.getPaddingTop();
    this.mRefreshState = 1;
    addHeaderView(this.mRefreshView);
    super.setOnScrollListener(this);
    this.mRefreshViewHeight = this.mRefreshView.getMeasuredHeight();
  }

  private void measureView(View paramView)
  {
    int k;
    ViewGroup.LayoutParams localLayoutParams = paramView.getLayoutParams();
    if (localLayoutParams == null)
      localLayoutParams = new ViewGroup.LayoutParams(-1, -2);
    int i = ViewGroup.getChildMeasureSpec(0, 0, localLayoutParams.width);
    int j = localLayoutParams.height;
    if (j > 0)
      k = View.MeasureSpec.makeMeasureSpec(j, 1073741824);
    while (true)
    {
      paramView.measure(i, k);
      return;
      k = View.MeasureSpec.makeMeasureSpec(0, 0);
    }
  }

  private void resetHeader()
  {
    this.mRefreshState = 1;
    resetHeaderPadding();
    this.mRefreshViewText.setText(2131361829);
    this.mRefreshViewImage.setImageResource(2130837624);
    this.mRefreshViewImage.clearAnimation();
    this.mRefreshViewImage.setVisibility(8);
    this.mRefreshViewProgress.setVisibility(8);
    this.mRefreshViewText.setVisibility(8);
    this.mRefreshViewLastUpdated.setVisibility(8);
  }

  private void resetHeaderPadding()
  {
  }

  protected void onAttachedToWindow()
  {
    super.onAttachedToWindow();
    setSelection(1);
  }

  public void onRefresh()
  {
    if (this.mOnRefreshListener != null)
    {
      this.mOnRefreshListener.onRefresh();
      resetHeader();
    }
  }

  public void onRefreshComplete()
  {
    Log.d("PullToRefreshExpandableListView", "onRefreshComplete");
    resetHeader();
    if (!(this.firstLoad))
    {
      setLastUpdated(null);
      this.firstLoad = false;
    }
    if (this.mRefreshView.getBottom() > 0)
    {
      invalidateViews();
      setSelection(1);
    }
  }

  public void onRefreshComplete(CharSequence paramCharSequence)
  {
    setLastUpdated(paramCharSequence);
    onRefreshComplete();
  }

  public void onScroll(AbsListView paramAbsListView, int paramInt1, int paramInt2, int paramInt3)
  {
    if ((this.mCurrentScrollState == 1) && (this.mRefreshState != 4))
      if (paramInt1 == 0)
      {
        this.mRefreshViewText.setVisibility(0);
        this.mRefreshViewImage.setVisibility(0);
        this.mRefreshViewLastUpdated.setVisibility(0);
        if ((((this.mRefreshView.getBottom() > 20 + this.mRefreshViewHeight) || (this.mRefreshView.getTop() >= 0))) && (this.mRefreshState == 2))
        {
          this.mRefreshViewText.setText(2131361827);
          setLastUpdated(null);
          this.mRefreshViewImage.clearAnimation();
          this.mRefreshViewImage.startAnimation(this.mFlipAnimation);
          this.mRefreshState = 3;
        }
      }
    while (true)
    {
      do
        while (true)
        {
          while (true)
          {
            do
              while (true)
              {
                while (true)
                {
                  if (this.mOnScrollListener != null)
                    this.mOnScrollListener.onScroll(paramAbsListView, paramInt1, paramInt2, paramInt3);
                  return;
                  if ((this.mRefreshState != 3) || (this.mRefreshView.getBottom() >= 20 + this.mRefreshViewHeight))
                    break;
                  this.mRefreshViewImage.setVisibility(8);
                  resetHeader();
                }
                if ((this.mRefreshView.getBottom() != this.mRefreshViewHeight) || (this.mRefreshState != 1))
                  break;
                this.mRefreshViewImage.setVisibility(8);
                resetHeader();
              }
            while ((this.mRefreshView.getBottom() >= 20 + this.mRefreshViewHeight) || (this.mRefreshState != 1));
            this.mRefreshViewText.setText(2131361826);
            this.mRefreshState = 2;
            this.mRefreshViewImage.clearAnimation();
            this.mRefreshViewImage.startAnimation(this.mReverseFlipAnimation);
          }
          this.mRefreshViewImage.setVisibility(8);
          resetHeader();
        }
      while ((this.mCurrentScrollState != 2) || (paramInt1 != 0) || (this.mRefreshState == 4));
      setSelection(1);
    }
  }

  public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt)
  {
    this.mCurrentScrollState = paramInt;
    if (this.mOnScrollListener != null)
      this.mOnScrollListener.onScrollStateChanged(paramAbsListView, paramInt);
  }

  public boolean onTouchEvent(MotionEvent paramMotionEvent)
  {
    int i = (int)paramMotionEvent.getY();
    switch (paramMotionEvent.getAction())
    {
    default:
    case 1:
    case 0:
    case 2:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          do
            while (true)
            {
              do
              {
                return super.onTouchEvent(paramMotionEvent);
                if (!(isVerticalScrollBarEnabled()))
                  setVerticalScrollBarEnabled(true);
              }
              while ((getFirstVisiblePosition() != 0) || (this.mRefreshState == 4));
              if (((this.mRefreshView.getBottom() <= this.mRefreshViewHeight) && (this.mRefreshView.getTop() < 0)) || (this.mRefreshState != 3))
                break;
              this.mRefreshState = 4;
              prepareForRefresh();
              onRefresh();
            }
          while ((this.mRefreshView.getBottom() >= this.mRefreshViewHeight) && (this.mRefreshView.getTop() >= 0));
          resetHeader();
          setSelection(1);
        }
        this.mLastMotionY = i;
      }
      applyHeaderPadding(paramMotionEvent);
    }
  }

  public void prepareForRefresh()
  {
    resetHeaderPadding();
    this.mRefreshViewImage.setVisibility(8);
    this.mRefreshViewImage.setImageDrawable(null);
    this.mRefreshViewProgress.setVisibility(0);
    this.mRefreshViewText.setText(2131361828);
    this.mRefreshState = 4;
  }

  public void setAdapter(ListAdapter paramListAdapter)
  {
    super.setAdapter(paramListAdapter);
    setSelection(1);
  }

  public void setLastUpdated(CharSequence paramCharSequence)
  {
    this.mRefreshViewLastUpdated.setVisibility(0);
    if (paramCharSequence != null)
      this.mRefreshViewLastUpdated.setText(paramCharSequence);
    while (true)
    {
      return;
      String str = "最后更新: " + DateUtil.dateToStringByFormat(new Date(), "MM月dd日 HH时mm分");
      this.mRefreshViewLastUpdated.setText(str);
    }
  }

  public void setOnRefreshListener(OnRefreshListener paramOnRefreshListener)
  {
    this.mOnRefreshListener = paramOnRefreshListener;
  }

  public void setOnScrollListener(AbsListView.OnScrollListener paramOnScrollListener)
  {
    this.mOnScrollListener = paramOnScrollListener;
  }

  private class OnClickRefreshListener
  implements View.OnClickListener
  {
    public void onClick()
    {
      if (PullToRefreshExpandableListView.access$0(this.this$0) != 4)
      {
        this.this$0.prepareForRefresh();
        this.this$0.onRefresh();
      }
    }
  }

  public static abstract interface OnRefreshListener
  {
    public abstract void onRefresh();
  }
}